package com.ja.openchat.ui.me;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Switch;

import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.ui.base.JaBaseActivity;
import com.zy.openchat.Constants;
import com.zy.openchat.core.model.setting.LocalSettingEvent;
import com.zy.openchat.util.SPUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;

public class UseMobileNetworkActivity extends JaBaseActivity {

    public static Bundle newInstance(int type){
        Bundle bundle = new Bundle();
        bundle.putInt("type",type);
        return bundle;
    }

    @BindView(R.id.autoDownloadMediaSwitch)
    Switch autoDownloadMediaSwitch;
    @BindView(R.id.autoDownloadImageSwitch)
    Switch autoDownloadImageSwitch;
    @BindView(R.id.autoDownloadVideoSwitch)
    Switch autoDownloadVideoSwitch;
    @BindView(R.id.autoDownloadFileSwitch)
    Switch autoDownloadFileSwitch;
    private String networkType = Constants.SPU_FOR_USE_MOBILE_NETWORK;
    public static final String SPE_LABLE = ",";
    public static final String ALL_OPEN = String.format("%d%s%d%s%d",Constants.NETWORK_FOR_IMAGE,SPE_LABLE,Constants.NETWORK_FOR_VIDEO,SPE_LABLE,Constants.NETWORK_FOR_FILE);
    @Override
    public void initData() {
        int type = 0;
        if(getIntent()!=null){
            type = getIntent().getIntExtra("type",0);
        }
        setTitle(type==0?getString(R.string.when_using_mobile_data):getString(R.string.when_using_wifi));
        networkType = type==0?Constants.SPU_FOR_USE_MOBILE_NETWORK:Constants.SPU_FOR_USE_WIFI_NETWORK;
        String value = SPUtils.getString(this, networkType,ALL_OPEN);

        autoDownloadMediaSwitch.setChecked(false);
        autoDownloadImageSwitch.setChecked(false);
        autoDownloadVideoSwitch.setChecked(false);
        autoDownloadFileSwitch.setChecked(false);
        if(!TextUtils.isEmpty(value)){
            autoDownloadMediaSwitch.setChecked(true);
            if(value.contains(String.valueOf(Constants.NETWORK_FOR_IMAGE))){
                autoDownloadImageSwitch.setChecked(true);
            }
            if(value.contains(String.valueOf(Constants.NETWORK_FOR_VIDEO))){
                autoDownloadVideoSwitch.setChecked(true);
            }
            if(value.contains(String.valueOf(Constants.NETWORK_FOR_FILE))){
                autoDownloadFileSwitch.setChecked(true);
            }
        }
        autoDownloadMediaSwitch.setOnClickListener(switchClick);
        autoDownloadImageSwitch.setOnClickListener(switchClick);
        autoDownloadVideoSwitch.setOnClickListener(switchClick);
        autoDownloadFileSwitch.setOnClickListener(switchClick);
    }

    private View.OnClickListener switchClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean isImageCheck = autoDownloadImageSwitch.isChecked();
            boolean isVideoheck = autoDownloadVideoSwitch.isChecked();
            boolean isFileCheck = autoDownloadFileSwitch.isChecked();
            switch(v.getId()){
                case R.id.autoDownloadMediaSwitch:
                    boolean isAllCheck = autoDownloadMediaSwitch.isChecked();
                    autoDownloadImageSwitch.setChecked(isAllCheck);
                    autoDownloadVideoSwitch.setChecked(isAllCheck);
                    autoDownloadFileSwitch.setChecked(isAllCheck);
                    isImageCheck = isAllCheck;
                    isVideoheck = isAllCheck;
                    isFileCheck = isAllCheck;
                    break;
                case R.id.autoDownloadImageSwitch:
                    if(isImageCheck){
                        autoDownloadMediaSwitch.setChecked(true);
                    }else if(!isVideoheck&&!isFileCheck){
                        autoDownloadMediaSwitch.setChecked(false);
                    }
                    break;
                case R.id.autoDownloadVideoSwitch:
                    if(isVideoheck){
                        autoDownloadMediaSwitch.setChecked(true);
                    }else if(!isImageCheck&&!isFileCheck){
                        autoDownloadMediaSwitch.setChecked(false);
                    }
                    break;
                case R.id.autoDownloadFileSwitch:
                    if(isFileCheck){
                        autoDownloadMediaSwitch.setChecked(true);
                    }else if(!isImageCheck&&!isVideoheck){
                        autoDownloadMediaSwitch.setChecked(false);
                    }
                    break;
            }
            String saveValue = "";
            if(isImageCheck){
                saveValue = String.valueOf(Constants.NETWORK_FOR_IMAGE);
            }
            if(isVideoheck){
                saveValue = saveValue+(TextUtils.isEmpty(saveValue)?"":SPE_LABLE)+String.valueOf(Constants.NETWORK_FOR_VIDEO);
            }
            if(isFileCheck){
                saveValue = saveValue+(TextUtils.isEmpty(saveValue)?"":SPE_LABLE)+String.valueOf(Constants.NETWORK_FOR_FILE);
            }
            SPUtils.putString(getApplication(),networkType,saveValue);
            SettingManager.getInstance().initNetworkDownload();
            EventBus.getDefault().post(new LocalSettingEvent(networkType));
        }
    };

    @Override
    protected boolean isHaveButterKnife() {
        return true;
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_use_mobile_network;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }
}
